package qst.com.bacK.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import qst.com.bacK.Constant;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.pojo.entity.AssessmentType;
import qst.com.bacK.pojo.entity.Schedule;
import qst.com.bacK.pojo.entity.Team;
import qst.com.bacK.pojo.entity.User;
import qst.com.bacK.service.AssessmentService;
import qst.com.bacK.service.ClassTeamService;
import qst.com.bacK.service.ScheduleService;
import qst.com.bacK.utils.RequestUtil;
import qst.com.bacK.utils.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Author:   msh
 * Date:     2024/10/14 下午2:23
 * Description:
 */

@Controller
@RequestMapping("/schedule")
public class ScheduleController {
    @Autowired
    private ClassTeamService classTeamService;
    @Autowired
    private AssessmentService assessmentService;
    @Autowired
    private ScheduleService scheduleService;

    // 创建
    @RequestMapping("/create.action")
    public void create(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER);
        Schedule schedule = (Schedule) req.getAttribute("schedule");
        if (schedule == null) {
            schedule = new Schedule();
            Date today = new Date(System.currentTimeMillis());
            SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            schedule.setBeginDate(stf.format(today));
            schedule.setEndDate(stf.format(today));

            req.setAttribute("schedule", schedule);
        }

        List<Team> teamList = classTeamService.findByCreator(user.getId());
        req.setAttribute("teamList", teamList);
        List<AssessmentType> assessmentList = assessmentService.findAllAssessment();
        req.setAttribute("assessmentList", assessmentList);

        WebUtil.forward(req, resp, "/schedule/create.jsp");
    }

    // 删除
    @RequestMapping("/delete.action")
    public void delete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Integer id = RequestUtil.getInt(req, "id");
        try {

            scheduleService.deleteSchedule(id);
        } catch (ExamException ex) {
            ex.printStackTrace();
            req.getSession().setAttribute("_error_key", ex.getMessage());
        }
        resp.sendRedirect("/schedule/list.action");
    }

    // 编辑
    @RequestMapping("/edit.action")
    public void edit(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER);
        Integer id = RequestUtil.getInt(req, "id");
        Schedule schedule = (Schedule) req.getAttribute("schedule");
        if (schedule == null) {
            schedule = scheduleService.findById(id);
            req.setAttribute("schedule", schedule);
        }

        List<Team> teamList = classTeamService.findByCreator(user.getId());
        req.setAttribute("teamList", teamList);
        List<AssessmentType> assessmentList = assessmentService.findAllAssessment();
        req.setAttribute("assessmentList", assessmentList);
        WebUtil.forward(req, resp, "/schedule/edit.jsp");
    }

    // 列表
    @RequestMapping("/list.action")
    public void list(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER);

        List<Schedule> scheduleList = scheduleService.findByCreator(user);
        req.setAttribute("scheduleList", scheduleList);

        WebUtil.forward(req, resp, "/schedule/list.jsp");
    }

    // 保存
    @RequestMapping("/save.action")
    public void save(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Schedule schedule = new Schedule();
        try {
            User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER);
            schedule.setBeginDate(RequestUtil.getString(req, "beginDate"));
            schedule.setEndDate(RequestUtil.getString(req, "endDate"));
            schedule.setDuration(RequestUtil.getInt(req, "duration"));
            schedule.setCreateDate(RequestUtil.getDate(req, "createDate"));
            schedule.setStatus(1);
            schedule.setAssessmentId(RequestUtil.getInt(req, "assessmentId"));
            schedule.setTeamId(RequestUtil.getInt(req, "teamId"));
            schedule.setQuestionNumber(RequestUtil.getInt(req, "questionNumber"));
            schedule.setCreatorId(user.getId());
            scheduleService.saveSchedule(schedule);
            resp.sendRedirect("/schedule/view.action?id=" + schedule.getId());
        } catch (ExamException ex) {
            ex.printStackTrace();
            req.getSession().setAttribute("_error_key", ex.getMessage());
            req.setAttribute("schedule", schedule);
            WebUtil.forward(req, resp, "/schedule/create.action");
        }
    }

    // 更新
    @RequestMapping("/update.action")
    public void update(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Schedule schedule = new Schedule();
        try {
            User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER);
            schedule.setId(RequestUtil.getInt(req, "id"));
            schedule.setBeginDate(RequestUtil.getString(req, "beginDate"));
            schedule.setEndDate(RequestUtil.getString(req, "endDate"));
            schedule.setDuration(RequestUtil.getInt(req, "duration"));
            schedule.setCreateDate(RequestUtil.getDate(req, "createDate"));
            schedule.setStatus(RequestUtil.getInt(req, "status"));
            schedule.setAssessmentId(RequestUtil.getInt(req, "assessmentId"));
            schedule.setTeamId(RequestUtil.getInt(req, "teamId"));
            schedule.setQuestionNumber(RequestUtil.getInt(req, "questionNumber"));
            schedule.setCreatorId(user.getId());
            scheduleService.updateSchedule(schedule);
            resp.sendRedirect("/schedule/view.action?id=" + schedule.getId());
        } catch (ExamException ex) {
            ex.printStackTrace();
            req.getSession().setAttribute("_error_key", ex.getMessage());
            req.setAttribute("schedule", schedule);
            WebUtil.forward(req, resp, "/schedule/update.action");
        }
    }

    //view
    @RequestMapping("/view.action")
    public void view(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Integer id = RequestUtil.getInt(req, "id");
        Schedule schedule = scheduleService.findById(id);
        req.setAttribute("schedule", schedule);
        WebUtil.forward(req, resp, "/schedule/view.jsp");
    }
}